//
//  DispatchQueue+.swift
//  XiabuMember
//
//  Created by WangYao on 2023/3/14.
//

import Foundation

public extension DispatchQueue {
    
    // 优先级userInteractive >  userInitiated > default > utility > background > unspecified
    // 1、使用global(),系统会默认给予Default优先级
    // 2、使用global(qos:xxx)自定义自己想要的优先级
    static var `default`: DispatchQueue { return DispatchQueue.global(qos: .`default`) }
    static var userInteractive: DispatchQueue { return DispatchQueue.global(qos: .userInteractive) }
    static var userInitiated: DispatchQueue { return DispatchQueue.global(qos: .userInitiated) }
    static var utility: DispatchQueue { return DispatchQueue.global(qos: .utility) }
    static var background: DispatchQueue { return DispatchQueue.global(qos: .background) }
    
    static let requestQueue = DispatchQueue(label: "com.apple.request", qos: .default)

    func after(_ delay: TimeInterval, execute closure: @escaping () -> Void) {
        asyncAfter(deadline: .now() + delay, execute: closure)
    }
    
    private static var _onceTracker = [String]()
    public class func once(block:() -> Void) {
        let token = UUID().uuidString
        objc_sync_enter(self)
        defer { objc_sync_exit(self) }
        
        if _onceTracker.contains(token) {
            return
        }
        _onceTracker.append(token)
        block()
    }
}
